package m.jclecai.com.fragment.bifen;


import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.squareup.okhttp.Request;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.MainActivity;
import m.jclecai.com.adapter.BiFenChildChildAdapter;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.bifen.MatchEvents;
import m.jclecai.com.bean.bifen.MatchTeam;
import m.jclecai.com.callback.GetMatchNameBack;
import m.jclecai.com.inter.IGetMatchsNames;
import m.jclecai.com.utils.http.HttpManager;


import static m.jclecai.com.constant.Constant_Url.FOOTBALL_MATCH_RESULT;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BiFenChild_ChildFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BiFenChild_ChildFragment extends BaseFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private ArrayList<MatchTeam> teamList;
    private ArrayList<MatchEvents> eventList;
    private JSONArray matches=null;
    private JSONObject teams=null;
    private JSONObject odds=null;
    private JSONObject events=null;
    private BiFenChildChildAdapter biFenChildChildAdapter;
    private ProgressDialog progressDialog;

    @BindView(R.id.rv_bifen_child_child)
    RecyclerView rv_bifen_child_child;

    public BiFenChild_ChildFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BiFenChild_ChildFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BiFenChild_ChildFragment newInstance(String param1, String param2) {
        BiFenChild_ChildFragment fragment = new BiFenChild_ChildFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }


    }


    @Override
    public void initData() {
        super.initData();
        //初始化数据
        teamList=new ArrayList<>();
        eventList=new ArrayList<>();
        progressDialog=new ProgressDialog(getActivity());
        progressDialog.setMessage("数据加载中,请稍后...");
        progressDialog.show();
        getBiFenData();

        rv_bifen_child_child.setLayoutManager(new LinearLayoutManager(getActivity()));

        rv_bifen_child_child.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));

        //添加数据
        biFenChildChildAdapter = new BiFenChildChildAdapter(getActivity(),teams,matches,events);
        rv_bifen_child_child.setAdapter(biFenChildChildAdapter);

    }

    private void getBiFenData() {
        HttpManager.getAsyn(FOOTBALL_MATCH_RESULT, true, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Log.v("params_fail", "onFailure  ==" + e.getLocalizedMessage());
            }

            @Override
            public void onResponse(String response) {
                progressDialog.dismiss();
                Log.v("params_success", "onResponse  ==" + response);
                if (eventList.size()>0){
                    eventList.clear();
                }
                if (teamList.size()>0){
                    teamList.clear();
                }
                try {
                    JSONObject matchData = new JSONObject(response);
                    //球队列表
                    teams = matchData.getJSONObject("teams");
                    // 赛事列表
                    events = matchData.getJSONObject("events");
                    //单场比赛
                    matches = matchData.getJSONArray("matches");
                    //指数字段
                    odds  = matchData.getJSONObject("odds");

                    String[]matchs=new String[matches.length()];
                    for (int i=0;i<matches.length();i++){
                        JSONArray jsonArray = matches.getJSONArray(i);
                        JSONObject jsonObject = events.getJSONObject(jsonArray.getInt(1) + "");
                        matchs[i]= jsonObject.getString("name_zh");
                    }
                    GetMatchNameBack.setMatchsNames(matchs);
//                    Iterator<String> teamKeys = teams.keys();
//                    if (teamKeys.hasNext()){
//                        JSONObject team = teams.getJSONObject(teamKeys.next().toString());
//                        MatchTeam matchTeam = new MatchTeam(team.getInt("id") + "",
//                                team.getString("name_zh"),
//                                team.getString("name_zht"),
//                                team.getString("name_en"),
//                                team.getString("logo"),
//                                team.getInt("matchevent_id") + ""
//                        );
//                        teamList.add(matchTeam);
//                    }

//                    Iterator<String> eventKeys = events.keys();
//                    if (eventKeys.hasNext()){
//                        JSONObject event = events.getJSONObject(eventKeys.next().toString());
//                        MatchEvents matchEvents = new MatchEvents(event.getInt("id") + "",
//                                event.getString("name_zh"),
//                                event.getString("short_name_zh"),
//                                event.getString("name_zht"),
//                                event.getString("short_name_zht"),
//                                event.getString("name_en"),
//                                event.getString("short_name_en"),
//                                event.getString("logo"));
//                        eventList.add(matchEvents);
//                    }


//                    //添加数据
                    biFenChildChildAdapter.addData(teams,matches,events);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_bi_fen_child__child;
    }




}
